<template>
  <Table stripe border :columns="tableHeader" :data="dataList" :class="tableName">
    <template slot-scope="{ index }" slot="number">
      {{offset + index + 1}}
    </template>
    <!-- carousel -->
    <template slot-scope="{ index }" slot="index">
      {{ index }}
    </template>
    <template slot-scope="{ row }" slot="img">
      <img :src="row.img"/>
    </template>
    <!-- type -->
    <template slot-scope="{ row, index }" slot="update">
      <Button v-if="row.parentId" type="info" size="small" @click="editParent(row)">Edit</Button>
    </template>
    <!-- tag -->
    <template slot-scope="{ row, index }" slot="style">
      <a :style="{'color':row.color}">{{row.name}}</a>
    </template>
    <!-- link -->
    <template slot-scope="{ row }" slot="picture">
      <img :src="row.picture" />
    </template>
    <template slot-scope="{ row }" slot="time">
      <span v-text="createTime(row.createTime)"></span>
    </template>
    <template slot-scope="{ row, index }" slot="link-action">
      <Button v-if="!row.allow" type="primary" size="small" style="margin-right: 5px" @click="allow(row)">allow</Button>
      <Button type="error" size="small" @click="remove(row)">Delete</Button>
    </template>
    <!-- common -->
    <template slot-scope="{ row, index }" slot="action">
      <Button type="primary" size="small" style="margin-right: 5px" @click="edit(row.id)">Edit</Button>
      <Button type="error" size="small" @click="remove(row)">Delete</Button>
    </template>
  </Table>
</template>

<script>
import { formatDate } from 'utils/utils'

export default {
  name: 'ManageTable',
  props: {
    tableName: String,
    tableHeader: {
      type: Object,
      default() {
        return {}
      }
    },
    dataList: {
      type: Object,
      default() {
        return {}
      }
    },
    currentPage: Number,
    pageSize: Number,
  },
  methods: {
    edit(id) {
      this.$emit('edit', id)
    },
    remove(row) {
      this.$emit('remove', row)
    },
    editParent(row) {
      this.$emit('editParent', row)
    },
    allow(row) {
      this.$emit('allow', row)
    },
    createTime(time) {
      return formatDate(new Date(time), "yyyy-MM-dd")
    }
  },
  computed: {
    offset() {
      return (this.currentPage - 1) * this.pageSize;
    }
  }
}
</script>

<style lang="scss">
.ivu-table-wrapper {
  margin: 1rem;
  .ivu-table-cell {
    padding: 0;
  }
  tr {
    th {
      padding: 14px 0 !important;
      text-align: center;
    }
    td {
      text-align: center;
      img {
        margin-top: 8px;
        width: 200px;
        height: 80px;
      }
      @media screen and (max-width: 500px) { 
        img {
          width: 110px;
          height: 60px;
        }
      }
    }
  }
}
</style>
<style lang="scss" scope>
.tag-manage{
  a {
    display: inline-block;
    font-size: 10px;
    line-height: 20px;
    padding: 0 10px;
    position: relative;
    &:before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      color: inherit;
      background: currentColor;
      border-radius: 20px;
      opacity: .15;
    }
  }
  
  //标签管理列表
  colgroup col {
    &:nth-child(6n-5){ width: 40px; }
    &:nth-child(6n-4){ width: 90px; }
    &:nth-child(6n-3){ width: 90px; }
    &:nth-child(6n-2){ width: 90px; }
    &:nth-child(6n-1){ width: 50px; }
    &:nth-child(6n){ width: 80px; }
  }
  @media screen and (max-width: 500px) { 
    // 设置列宽
    colgroup col {
      &:nth-child(6n-5){ width: 30px; }
      &:nth-child(6n-4){ width: 70px; }
      &:nth-child(6n-3){ width: 50px; }
      &:nth-child(6n-1){ width: 50px; }
    }
    // 设置列隐藏
    colgroup col, tr th, tr td{
      &:nth-child(6n-2){ display: none; }
    }
  }
}
// 博客管理列表
.blog-manage {
  colgroup {
    col{
      &:nth-child(8n-7){ width: 40px; }
      &:nth-child(8n-5){ width: 90px; }
      &:nth-child(8n-4){ width: 50px; }
      &:nth-child(8n-3){ width: 50px; }
      &:nth-child(8n-2){ width: 50px; }
      &:nth-child(8n-1){ width: 130px; }
      &:nth-child(8n){ width: 100px; } 
    }
  }
  @media screen and (max-width: 700px) { 
    // 设置列宽
    colgroup col {
      &:nth-child(8n-7){ width: 20px; }
      &:nth-child(8n-6){ width: 50px; }
      &:nth-child(8n-5){ width: 30px; }   
      &:nth-child(8n-3){ width: 20px; }
      &:nth-child(8n-2){ width: 20px; }
      &:nth-child(8n){ width: 40px; }
    }
    // 设置列隐藏
    colgroup col, tr th, tr td{
      &:nth-child(8n-4){ display: none; }
      &:nth-child(8n-1){ display: none; }
    }
  }
}
// 分类管理列表
.type-manage {
  colgroup col{
    &:nth-child(6n-5){ width: 30px; }
    &:nth-child(6n-4){ width: 90px; }
    &:nth-child(6n-3){ width: 90px; }
    &:nth-child(6n-2){ width: 40px; }
    &:nth-child(6n-1){ width: 60px; }
    &:nth-child(6n){ width: 100px; } 
  }
  @media screen and (max-width: 500px) { 
    // 设置列宽
    colgroup col {
      &:nth-child(6n-5){ width: 30px; }
      &:nth-child(6n-4){ width: 80px; }
      &:nth-child(6n-3){ width: 50px; }
      &:nth-child(6n-1){ width: 40px; }
      &:nth-child(6n){ width: 80px; } 
    }
  }
}
// 轮播图列表管理
.carousel-manage {
  colgroup col {
    &:nth-child(5n-4){ width: 30px; }
    &:nth-child(5n-3){ width: 80px; }
    &:nth-child(5n-2){ width: 130px; }
    &:nth-child(5n-1){ width: 90px; }
    &:nth-child(5n){ width: 70px; }
  }
  @media screen and (max-width: 500px) { 
    // 设置列宽
    colgroup col {
      &:nth-child(5n-4){ width: 30px; }
      &:nth-child(5n-3){ width: 80px; }
      &:nth-child(5n-2){ width: 130px; }
      &:nth-child(5n){ width: 70px; }
    }
    // 设置列隐藏
    colgroup col, tr th, tr td{
      &:nth-child(5n-1){ display: none; }
    }
  }
}
//友链列表管理
.link-manage {
  img {
    margin: 10px;
    width: 66px !important;
    height: 66px !important;
    border-radius: 35px;
  }
  colgroup col {
    &:nth-child(7n-6){ width: 30px; }
    &:nth-child(7n-5){ width: 80px; }
    &:nth-child(7n-4){ width: 100px; }
    &:nth-child(7n-3){ width: 100px; }
    &:nth-child(7n-2){ width: 70px; }
    &:nth-child(7n-1){ width: 70px; }
    &:nth-child(7n){ width: 70px; }
  }
  @media screen and (max-width: 650px) { 
    // 设置列宽
    colgroup col {
      &:nth-child(7n-6){ width: 30px; }
      &:nth-child(7n-5){ width: 65px; }
      &:nth-child(7n-3){ width: 80px; }
      &:nth-child(7n-2){ width: 85px; }
      &:nth-child(7n){ width: 65px; }
    }
    // 设置列隐藏
    colgroup col, tr th, tr td{
      &:nth-child(7n-4){ display: none; }
      &:nth-child(7n-1){ display: none; }
    }
  }
}
</style>